#!/bin/bash
#    am-kb-pipemenu - an Openbox pipemenu for displaying keybinds
#    Copyright (C) 2015 damo    <damo@bunsenlabs.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# NB The keybinds in the menu are clickable, except for the Openbox
# commands, which just exit the menu

# am-kb is used to parse rc.xml and send the output to a textfile


HELP='    am-kb-pipemenu an Openbox Pipe Menu for displaying keybinds
    It should normally be called from an openbox menu.

Options (usually passed from the menu):
    -h --help   show this message
    --keybinds  display keybindings defined in rc.xml as a menu
    (The keybinds in the menu are clickable, except for the Openbox
     commands, which just exit the menu.)
    --rcedit    backup and edit rc.xml

If am-keyboard-pipemenu is called with no options (the usual method),
it will output an xml openbox pipemenu for displaying keybindings.

NOTE: Some of the functionality of this pipemenu depends on the script
/usr/bin/am-kb which is provided by the package "arcolinux-utilities".
'
# look for a help option somewhere
for i in "$@"
do
    case "$i" in
    -h|--help)
        echo "$HELP"
        exit 0
        ;;
    esac
done

BL_COMMON_LIBDIR="/usr/lib/arcolinux/common"
USR_CFG_DIR="$HOME/.config/openbox"
KBSCRIPT="am-kb"
RCPATH="$USR_CFG_DIR/rc.xml"
KBTEXT="$USR_CFG_DIR/kbinds.txt"

if ! . "$BL_COMMON_LIBDIR/am-include.cfg" 2> /dev/null; then
    echo $"Error: Failed to locate am-include.cfg in $BL_COMMON_LIBDIR" >&2
    exit 1
fi

# test if am-kb exists in path
if ! type "$KBSCRIPT" &> /dev/null;then
    menuStart
    menuSeparator "$KBSCRIPT is not available. Try installing ArcoLinux-utilities."
    menuEnd
    exit 1
fi

if [[ $1 = '--rcedit' ]];then # "Backup & Edit rc.xml" is chosen in menu
    # backup rc.xml first
    NOW=$(date +"%Y%m%d-%H%M")
    RCBKP="$RCPATH.$NOW"
    cp "$RCPATH" "$RCBKP"
    geany "$RCPATH"        # open rc.xml in default editor
    exit
fi

if [[ $1 = '--keybinds' ]];then
    # run script to write kbinds.txt, suppress terminal output
    "$KBSCRIPT" > /dev/null
    menuStart
    menuSeparator "Openbox window commands"
    while read -r a b c;do      # write out Openbox commands (not clickable)
        if [[ "$a" = "o" ]];then
            curItem=$(printf "%-20s %s" "$b" "$c")
            menuItem "$curItem" "echo >/dev/null 2>&1"
        fi
    done < "$KBTEXT"

    menuSeparator
    menuSeparator "'Run' commands"
    while read -r a b c;do      # write out 'Run' commands (clickable)
        if [[ "$a" = "x" ]];then
            curItem=$(printf "%-20s %s" "$b" "$c")
            menuItem "$curItem" "$c"
        fi
    done < "$KBTEXT"
    menuEnd
    exit
fi

# pipemenu
menuStart
    menuItem "Backup and Edit rc.xml" "am-kb-pipemenu --rcedit"
    menuSeparator
    menuItem "Display Keybinds in new window" "am-kb --gui"
    echo '        <menu execute="am-kb-pipemenu --keybinds" id="Display keybinds" label="Display Keybinds in menu"/>'
menuEnd

exit 0
